//
//  TransformButton.m
//
//  Created by Zhenglinqin on 14-7-15.
//  Copyright (c) 2014年  Binglin All rights reserved.
//

#import "TransformButton.h"

@implementation TransformButton

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self config];
    }
    return self;
}

- (void)config{
    self.normal = YES;
    self.radius = M_PI;
    self.transformTime = 0.3f;
//    [self addTarget:self action:@selector(doTouchUpInside) forControlEvents:UIControlEventTouchUpInside];
}

- (void)awakeFromNib{
    [super awakeFromNib];
    [self config];
}

- (void)doTouchUpInside{
    [self doTransform];
    if (self.insideblock) {
        self.insideblock(self);
    }
}

- (void)doTransform{
    [UIView animateWithDuration:self.transformTime * (self.radius / M_PI) delay:0.1
                        options:UIViewAnimationOptionCurveLinear
                     animations:^{
        CGAffineTransform transform = (self.normal)?
                         (CGAffineTransformMakeRotation(self.radius)):(CGAffineTransformIdentity);
        self.transformView.transform = transform;
    } completion:^(BOOL finished) {
        self.normal = !self.normal;
    }];
}

- (void)resetButton{
    if (self.normal == NO) {
        [self doTransform];
    }
}

- (void)doRotate:(BOOL)rotate{
    if (rotate) {
        [UIView animateWithDuration:self.transformTime * (self.radius / M_PI) delay:0.1
                            options:UIViewAnimationOptionCurveLinear
                         animations:^{
            CGAffineTransform transform = (rotate)?
                             (CGAffineTransformMakeRotation(self.radius)):(CGAffineTransformIdentity);
            self.transformView.transform = transform;
        } completion:^(BOOL finished) {
            self.normal = !rotate;
        }];
    }else{
        CGAffineTransform transform = (rotate)?(CGAffineTransformMakeRotation(self.radius)):(CGAffineTransformIdentity);
        [UIView animateWithDuration:self.transformTime * (self.radius / M_PI) animations:^{
            self.transformView.transform = transform;
        } completion:^(BOOL finished) {
            self.normal  = YES;
        }];
    }
}

- (UIView *)transformView{
    return self;
}

@end




@interface ArrowButton ()
{
    UIImageView *_arrowImageView;
}

@property (nonatomic, assign) BOOL selected;

@end
@implementation ArrowButton
@synthesize selected=_selected;

- (instancetype)initWithTitle:(NSString *)title ArrowImage:(UIImage *)image{
    if ((self = [ArrowButton buttonWithType:UIButtonTypeCustom])) {
        _arrowImageView = [[UIImageView alloc] initWithImage:image];
        _arrowImageView.highlightedImage = [[UIImage imageNamed:@"arrow_red_icon"] imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
        [self setTitle:title forState:UIControlStateNormal];
        [self addSubview:_arrowImageView];
        self.titleLabel.lineBreakMode = NSLineBreakByTruncatingTail;
        
    }
    return self;
}

- (UIImageView *)arrowImageView{
    return _arrowImageView;
}

- (UIView *)transformView{
    return _arrowImageView;
}

- (void)layoutSubviews{
    [super layoutSubviews];

    CGFloat maxLabelWidth = CGRectGetWidth(self.frame) - 2 * CGRectGetWidth(_arrowImageView.frame) - 2 * 10;
    CGSize size = [self.titleLabel sizeThatFits:CGSizeMake(maxLabelWidth, CGRectGetHeight(self.frame))];
    
    if (size.width > maxLabelWidth) {
        size.width = maxLabelWidth;
    }
    
    CGRect fTitle = self.titleLabel.frame;
    fTitle.size = size;
    fTitle.origin.x = (CGRectGetWidth(self.frame) - size.width)/2.0;
    
    _arrowImageView.frame = ({
        CGRect frame = _arrowImageView.frame;
        frame.origin.x = CGRectGetMaxX(self.titleLabel.frame) + 5;
        frame.origin.y = (CGRectGetHeight(self.frame) - CGRectGetHeight(_arrowImageView.frame))/2.0;
        frame;
    });
}

- (void)setSelected:(BOOL)selected{
    [super setSelected:selected];
    _selected = selected;
    _arrowImageView.highlighted = selected;
    if (selected) {
        _arrowImageView.tintColor = [UIColor blackColor];
    }else{
        _arrowImageView.tintColor = nil;
    }
    [self doRotate:selected];
}


@end
